package com.hot.hot_backend.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hot.hot_backend.pojo.HotUserback;
import com.hot.hot_backend.pojo.Vo;
import com.hot.hot_backend.service.impl.HotUserbackServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  管理员
 * </p>
 *
 * @author hot
 * @since 2024-07-17
 */
@CrossOrigin
@RestController
@RequestMapping("/hot_userback")
public class HotUserbackController {

    @Resource
    private HotUserbackServiceImpl userbackService;

    // 注册
    @PostMapping("/register")
    public Vo<HotUserback> register(@RequestBody HotUserback userback){
        // 查询是否有有重复的用户
        LambdaQueryWrapper<HotUserback> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HotUserback::getAcccount,userback.getAcccount());
        HotUserback one = userbackService.getOne(wrapper);
        if (one == null){
            // 表示没有被注册
            boolean b = userbackService.save(userback);
            if (b){
                return new Vo<>(b,"注册成功，请进行登录！");
            }else {
                return new Vo<>(b,"注册失败，请稍后重试！");
            }
        }else {
            // 已经被注册
            return new Vo<>(false,"该账号已经被注册请更换账号！");
        }
    }

    // 登录
    @PostMapping("/login")
    public Vo<HotUserback> login(@RequestBody HotUserback userback){
        LambdaQueryWrapper<HotUserback> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HotUserback::getAcccount,userback.getAcccount());
        wrapper.eq(HotUserback::getPassword,userback.getPassword());
        HotUserback one = userbackService.getOne(wrapper);
        if (one == null){
            // 表示账号或密码错误
            return new Vo<>(false,"账号或密码错误，请重新输入！");
        }else {
            return new Vo<>(one.getId(),true);
        }
    }

    // 修改密码
    @PostMapping("/updatePass")
    public Vo<HotUserback> updatePass(@RequestBody HotUserback userback){
        // 创建修改密码的条件
        UpdateWrapper<HotUserback> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",userback.getId());
        // 原来的旧密码
        wrapper.eq("password",userback.getAcccount());
        // 需要修改的密码
        wrapper.set("password",userback.getPassword());
        boolean b = userbackService.update(wrapper);
        if (b){
            // 表示修改成功
            return new Vo<>(b,"修改成功，请重新登录");
        }else {
            return new Vo<>(b,"修改密码失败，请稍后重试！");
        }
    }

    // 修改个人信息
    @PostMapping("/update")
    public Vo<HotUserback> update(@RequestBody HotUserback userback){
        // 直接修改需要的内容
        boolean b = userbackService.updateById(userback);
        // 判断是否修改成功
        if (b){
            return new Vo<>(b,"修改成功！");
        }else {
            return new Vo<>(b,"修改失败，请稍后重试！");
        }
    }

    // 查询个人信息
    @GetMapping("/queryOne/{id}")
    public Vo<HotUserback> queryOne(@PathVariable("id") String id){
        // 根据id查询出对应的数据
        LambdaQueryWrapper<HotUserback> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HotUserback::getId,id);
        HotUserback one = userbackService.getOne(wrapper);
        if (one == null){
            return new Vo<>(false,"查询失败，请稍后重试");
        }else {
            return new Vo<>(one);
        }
    }
}
